﻿<template id="@(ViewBag.GetCompTemplate("pwd_strategy"))">
    <div class="layout-page-content">
        <el-form ref="formRef" :rules="rules" :disabled="disabled" :model="record" :hide-required-asterisk="disabled" label-width="auto" label-suffix=" :" class="layout-page-tabs-form">
            <el-form-item label="密码格式策略" prop="lockFormat">
                <el-radio-group v-model="record.lockFormat">
                    <el-radio-button value="简单" label="简单"></el-radio-button>
                    <el-radio-button value="复杂" label="复杂"></el-radio-button>
                    <el-radio-button value="不限" label="不限"></el-radio-button>
                </el-radio-group>
                <div class="layout-page-form-info">
                    <el-icon><info-filled></info-filled></el-icon>
                    <span>简单：仅要求密码长度 8-20 位；</span>
                </div>
                <div class="layout-page-form-info">
                    <el-icon><info-filled></info-filled></el-icon>
                    <span>复杂：且必须包含大小写字母、数字、特殊字符；</span>
                </div>
            </el-form-item>
            <el-divider class="layout-page-form-top-border"></el-divider>
            <el-form-item label="试错锁定" prop="lockSwitch">
                <el-switch v-model="record.lockSwitch" active-text="开启" inactive-text="关闭"></el-switch>
                <div class="layout-page-form-info">
                    <el-icon><info-filled></info-filled></el-icon>
                    <span>登陆时多次尝试错误密码后，账号将进行限时锁定。</span>
                </div>
            </el-form-item>
            <el-form-item label="可尝试次数" prop="lockNumber">
                <el-input-number v-model="record.lockNumber" :min="1" :max="10"></el-input-number>
            </el-form-item>
            <el-form-item label="锁定时间(分钟)" prop="lockMinute">
                <el-input-number v-model="record.lockMinute" :min="1" :max="120"></el-input-number>
            </el-form-item>
            <el-divider class="layout-page-form-top-border"></el-divider>
            <el-form-item label="密码过期" prop="expireSwitch">
                <el-switch v-model="record.expireSwitch" active-text="开启" inactive-text="关闭"></el-switch>
                <div class="layout-page-form-info">
                    <el-icon><info-filled></info-filled></el-icon>
                    <span>密码使用超过一定时间后，需要修改新密码才能登录。</span>
                </div>
            </el-form-item>
            <el-form-item label="密码有效期(天)" prop="expireDay">
                <el-input-number v-model="record.expireDay" :min="1" :max="365"></el-input-number>
            </el-form-item>
            <el-divider class="layout-page-form-top-border"></el-divider>
            <el-form-item label="历史密码校验" prop="historicalSwitch">
                <el-switch v-model="record.historicalSwitch" active-text="开启" inactive-text="关闭"></el-switch>
                <div class="layout-page-form-info">
                    <el-icon><info-filled></info-filled></el-icon>
                    <span>修改新密码不可以与历史密码相同，最多可以向前追溯10次。</span>
                </div>
            </el-form-item>
            <el-form-item label="追溯次数" prop="historicalNumber">
                <el-input-number v-model="record.historicalNumber" :min="1" :max="10"></el-input-number>
            </el-form-item>
            <el-divider class="layout-page-form-top-border"></el-divider>
            <el-form-item>
                <el-button type="primary" v-on:click="save">保存设置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script type="text/javascript">
    var @(ViewBag.GetCompName("pwd_strategy")) = {
        template: "#@(ViewBag.GetCompTemplate("pwd_strategy"))", data() {
            return {
                path: "@(ViewBag.PagePath)",
                rules: {
                    lockNumber: { required: true, message: "请填写可尝试次数" },
                    lockMinute: { required: true, message: "请填写锁定时间(分钟)" },
                    expireDay: { required: true, message: "请填写密码有效期(天)" },
                    historicalNumber: { required: true, message: "请填写追溯次数" }
                },
                record: {
                    lockFormat: "简单",
                    lockSwitch: true,
                    lockNumber: 5,
                    lockMinute: 10,
                    expireSwitch: true,
                    expireDay: 365,
                    historicalSwitch: true,
                    historicalNumber: 3
                },
                data: {
                    lockFormat: { Code: "pwd_strategy_format", Title: "账号安全策略——密码格式策略", Type: "String" },
                    lockSwitch: { Code: "pwd_strategy_lock_switch", Title: "账号安全策略——试错锁定", Type: "Boolean" },
                    lockNumber: { Code: "pwd_strategy_lock_number", Title: "账号安全策略——试错锁定：可尝试次数", Type: "Number" },
                    lockMinute: { Code: "pwd_strategy_lock_minute", Title: "账号安全策略——试错锁定：锁定时间(分钟)", Type: "Number" },
                    expireSwitch: { Code: "pwd_strategy_expire_switch", Title: "账号安全策略——密码过期", Type: "Boolean" },
                    expireDay: { Code: "pwd_strategy_expire_day", Title: "账号安全策略——密码过期：密码有效期(天)", Type: "Number" },
                    historicalSwitch: { Code: "pwd_strategy_historical_switch", Title: "账号安全策略——历史密码校验", Type: "Boolean" },
                    historicalNumber: { Code: "pwd_strategy_historical_number", Title: "账号安全策略——历史密码校验：追溯次数", Type: "Number" }
                },
                disabled: false
            }
        },
        methods: {
            init(){
                var that = this;
                app.func.post(`${that.path}/getdata`, { prefix: "pwd_strategy_" }, ".layout-page-tabs-form", result => {
                    if (result.success) {
                        var keys = Object.keys(that.record);
                        keys.forEach(key => {
                            var cfg = result.data.find(m => m.Code == that.data[key].Code);
                            if (cfg) {
                                switch (that.data[key].Type) {
                                    case "Boolean": that.record[key] = cfg.Content == "true"; break;
                                    case "Number": that.record[key] = Number(cfg.Content); break;
                                    default: that.record[key] = String(cfg.Content); break;
                                }
                            }
                        });
                    }
                    else { app.func.warning(result.message); }
                });
            },
            save(){
                var that = this;
                that.$refs.formRef.validate(isValid => {
                    if (isValid) {
                        var data = [], keys = Object.keys(that.record);
                        keys.forEach(key => {
                            data.push({ Code: that.data[key].Code, Title: that.data[key].Title, Content: String(that.record[key]) });
                        });
                        app.func.post(`${that.path}/savedata`, { data: JSON.stringify(data) }, ".layout-page-tabs-form", result => {
                            if (result.success) { app.func.message(result.message); } else { app.func.warning(result.message); }
                        });
                    }
                });
            }
        },
        mounted() { this.init(); }
    };
</script>